#! /usr/bin/python
import sys
from TOSSIM import *
from RadioCountMsg import *
#from tinyos.tossim.TossimApp import *

#n = NescApp()
#t = Tossim(n.variables.variables())
t = Tossim([])
m = t.mac();
r = t.radio();

energyfile = open("energy.txt", "w")


t.addChannel("RadioCountToLedsC", sys.stdout);
#t.addChannel("LedsC", sys.stdout);
t.addChannel("RoutingLayer", sys.stdout);
t.addChannel("TinyAMF", sys.stdout);

#t.addChannel("ENERGY_HANDLER", sys.stdout);

ticks = t.ticksPerSecond();

print "Ticks per second="+str(ticks);

for i in range(0, 10):	
  m = t.getNode(i);
  time = (31 + ticks / 100) * i + 1;
  m.bootAtTime(time);
  print "Node "+str(i)+" started at time="+str(time);


f = open("topo.txt", "r")
lines = f.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
  	r.add(int(s[0]), int(s[1]), float(s[2]))

noise = open("meyer-heavy.txt", "r")
lines = noise.readlines()
for line in lines:
  str = line.strip()
  if (str != ""):
    val = int(str)
    for i in range(0, 10):
      t.getNode(i).addNoiseTraceReading(val)

for i in range (0, 10):
  t.getNode(i).createNoiseModel()

t.addChannel("ENERGY_HANDLER", energyfile);


#===================================================================

#f1 = open("log.txt", "w")
#t.addChannel("TestChannel", f1);


routing = open("routing_table.txt", "r")
lines = routing.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
  	msg = RadioCountMsg()
	pkt = t.newPacket()	
	pkt.setType(msg.get_amType())
	#print "Delivering " + str(msg) + " to 0 at " + str(t.time() + 3);
	#for i in range(1, 4):
	m = t.getNode(0)
	#v = m.getVariable("RadioCountToLedsC.seq")
	msg.set_seq(999)
	#msg.set_hash(65040)
	#msg.set_auth(0)
	msg.set_payload(int(s[0]))
	#pkt.setSource(0)
	pkt.setDestination(int(s[1]))
	pkt.setData(msg.data)
	pkt.deliver(int(s[1]), t.time())

	for i in range(0, 1000):
	  t.runNextEvent();







#===================================================================


for i in range(0, 1100):
  t.runNextEvent();


"""

msg = RadioCountMsg()
pkt = t.newPacket()	
pkt.setType(msg.get_amType())

#print "Delivering " + str(msg) + " to 0 at " + str(t.time() + 3);
#for i in range(1, 4):
m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(64671)
msg.set_hash(65040)
msg.set_auth(0)
pkt.setSource(3)
pkt.setDestination(2)
pkt.setData(msg.data)
pkt.deliver(2, t.time())

for i in range(0, 1000):
  t.runNextEvent();


m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(345)
msg.set_hash(234)
msg.set_auth(0)
pkt.setSource(2)
pkt.setDestination(1)
pkt.setData(msg.data)
pkt.deliver(1, t.time())

for i in range(0, 30):
  t.runNextEvent();



m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(v.getData())
pkt.setDestination(2)
pkt.setData(msg.data)
pkt.deliver(2, t.time())


for i in range(0, 30):
  t.runNextEvent();

m = t.getNode(0)
v = m.getVariable("RadioCountToLedsC.counter")
msg.set_counter(v.getData())
pkt.setDestination(3)
pkt.setData(msg.data)
pkt.deliver(3, t.time())

for i in range(0, 30):
  t.runNextEvent();

"""


